﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using System.Collections;
using ZLCMS.Business.Config;
using System.Reflection;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.Doc
{
    public partial class Render : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this);
            if (IsPostBack)
            {
                RenderHtml();
            }
            else if (!IsPostBack)
            {
                InitializeContent();
            }
            
        }

        /// <summary>
        /// 设置field是否可见
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        protected String SetVisible(String field)
        {
            if (GetConfigPropertyValue(field))
            {
                return "";
            }
            else
            {
                return "style=\"display:none\"";
            }
        }

        /// <summary>
        /// 获取config的属性值
        /// </summary>
        /// <returns></returns>
        protected bool GetConfigPropertyValue(String field)
        {
            return true;
            //PropertyInfo property = _config.GetType().GetProperty(field);
            //return (bool)property.GetValue(_config, null);
        }


        protected void InitializeContent()
        {
            //doc_kind.Value = (String.IsNullOrEmpty(Request["Kind"]) ? "All" : Request["Kind"]);

            //_config = _configAcc.GetById(doc_kind.Value);

            //if (!String.IsNullOrEmpty(_config.SinglePathName))
            //{
            //    txtSingleName.Text = _config.SinglePathName;
            //}
            //else
            //{
            //    txtSingleName.Text = "/html/doc/doc_<#id#>.html";
            //}

            //if (!String.IsNullOrEmpty(_config.SingleTemplate))
            //{
            //    txtSingleTemplate.Text = _config.SingleTemplate;
            //}
            //else
            //{
            //    txtSingleTemplate.Text = "/template/CmsDocInfo.aspx";
            //}

            //chkRerender.Checked = (bool)_config.SingleRerender;
           
            //if (!String.IsNullOrEmpty(_config.ListPathName))
            //{
            //    txtListName.Text = _config.ListPathName;
            //}
            //else
            //{
            //    txtListName.Text = "/html/doc/list.html";
            //}

            //if (!String.IsNullOrEmpty(_config.ListTemplate))
            //{
            //    txtListTemplate.Text = _config.ListTemplate;
            //}
            //else
            //{
            //    txtListTemplate.Text = "/template/doclist.aspx";
            //}

            //if (!String.IsNullOrEmpty(_config.PagerPathName))
            //{
            //    txtPagerName.Text = _config.PagerPathName;
            //}
            //else
            //{
            //    txtPagerName.Text = "/html/doc/doc_page_<#PageIndex#>.html";
            //}

            //if (!String.IsNullOrEmpty(_config.PageTemplate))
            //{
            //    txtPagerTemplate.Text = _config.PageTemplate;
            //}
            //else
            //{
            //    txtPagerTemplate.Text = "/template/docpager.aspx";
            //}

            //if (_config.PagerSize > 0)
            //{
            //    txtPagerSize.Text = _config.PagerSize.ToString();
            //}
            //else
            //{
            //    txtPagerSize.Text = "20";
            //}


        }

        protected void SaveConfig()
        {
            //_config = _configAcc.GetById(doc_kind.Value);

            //_config.SinglePathName = txtSingleName.Text;
            //_config.SingleTemplate = txtSingleTemplate.Text;
            //_config.SingleRerender = chkRerender.Checked;

            //_config.ListPathName = txtListName.Text;
            //_config.ListTemplate = txtListTemplate.Text;

            //_config.PagerPathName = txtPagerName.Text;
            //_config.PageTemplate = txtPagerTemplate.Text;
            //_config.PagerSize = Int32.Parse(txtPagerSize.Text);

//             _configAcc.Save(_config);
//             _configAcc.Session.CommitChanges();
        }

        protected void RenderHtml()
        {
            //SaveConfig();
            //switch(render_flag.Value)
            //{
            //        //输出所有
            //    case "0":
            //        if ((bool)_config.SingleInfo)
            //        {
            //            RenderInfo();
            //        }
            //        if ((bool)_config.ListInfo)
            //        {
            //            RenderList();
            //        }
            //        if ((bool)_config.PagerInfo)
            //        {
            //            RenderPager();
            //        }
            //        break;
            //        //输出信息
            //    case "1":
            //        if ((bool)_config.SingleInfo)
            //        {
            //            RenderInfo();
            //        }
            //        break;
            //        //输出列表
            //    case "2":
            //        if ((bool)_config.ListInfo)
            //        {
            //            RenderList();
            //        }
            //        break;
            //        //输出分页
            //    case "3":
            //        if ((bool)_config.PagerInfo)
            //        {
            //            RenderPager();
            //        }
            //        break;
            //}
            _framePage.Alert(new ReturnCode() { statusCode = 200, message = "操作成功!" });
        }

        //protected IQuery CreateQuery()
        //{
        //    IQuery query = null;
        //    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
        //    {
        //        if (!chkRerender.Checked)
        //        {
        //            String hql = "select DocHtml from DocHtml a,CmsDocInfo b where a.DocId=b.Id and a.IsDone=false order by a.Id desc";
        //            session.CreateQuery(hql);
        //        }
        //        else
        //        {
        //            String hql = "select DocHtml from DocHtml a,CmsDocInfo b Where a.DocId = b.Id order by a.Id desc";
        //            session.CreateQuery(hql);
        //        }
        //    }

        //    return query;
        //}

        //protected void RenderInfo()
        //{
        //    int count = GetHtmlCount(chkRerender.Checked);

        //    for (int i = 0; i <= count / 20; i++)
        //    {
        //        RenderInfo(CreateQuery().SetFirstResult(i * 20).SetMaxResults(20).List<DocHtml>());
        //    }
        //    _htmlAcc.Session.CommitChanges();
        //}

        //protected void RenderInfo(IList<DocHtml> list)
        //{
        //    foreach (DocHtml html in list)
        //    {
        //        String file = Server.MapPath("~" + txtSingleName.Text.Replace("<#id#>", html.DocId.ToString()));

        //        String url = ServerUtil.GetRootURI() + txtSingleTemplate.Text.Replace("<#id#>", html.DocId.ToString());

        //        new TemplateRenderer() { TempUrl = url, Html = file }.Render();

        //        html.IsDone = DatabaseHelper.ConvertBool(true);
        //        html.OutputTime = DateTime.Now;
        //        html.OutputUrl = file;
        //        _htmlAcc.Save(html);
        //    }
        //}

        //protected int GetHtmlCount(bool idRerender)
        //{
        //    //select count(p) from Product p having p.price < avg(amount) group by p.id

        //    IQuery query = null;
        //    if (!idRerender)
        //    {
        //        String hql = "select count(a) from DocHtml a,CmsDocInfo b where a.DocId=b.Id and a.IsDone=false";
        //        _htmlAcc.Session.GetISession().CreateQuery(hql);
        //    }
        //    else
        //    {
        //        String hql = "select count(a) from DocHtml a,CmsDocInfo b Where a.DocId = b.Id";
        //        _htmlAcc.Session.GetISession().CreateQuery(hql);
        //    }
        //    return (int)query.UniqueResult();
        //}

        //protected void RenderList()
        //{
        //    TemplateRenderer render = new TemplateRenderer();

        //    render.Html = Server.MapPath("~" + txtListName.Text);
        //    render.TempUrl = ServerUtil.GetRootURI() + txtListTemplate.Text;
        //    render.Render();
        //}

        //protected void RenderPager()
        //{
        //    int pageSize = Int32.Parse(txtPagerSize.Text);

        //    int rowCount = (int)GetHtmlCount(true);

        //    int pageCount = rowCount / pageSize;
        //    for (int pageIndex = 0; pageIndex <= pageCount; pageIndex++)
        //    {
        //        String html = "~" + txtPagerName.Text.Replace("<#PageIndex#>", pageIndex.ToString());
        //        String url = ServerUtil.GetRootURI() +
        //            txtPagerTemplate.Text.Replace("<#PageIndex#>", pageIndex.ToString()).Replace("<#PageSize#>", pageSize.ToString());
        //        new TemplateRenderer() { TempUrl = url, Html = html }.Render();
        //    }
        //}
    }
}
